package StudyCode.summary.aclfile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.AclEntry;
import java.nio.file.attribute.AclEntryFlag;
import java.nio.file.attribute.AclEntryPermission;
import java.nio.file.attribute.AclFileAttributeView;
import java.util.List;
import java.util.Set;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class MaintainingFileACLUsingAclFileAttributeView {
	public static void main(String[] args) {
		Path path = Paths.get("E:/Copy", "1.txt");
		try {
			AclFileAttributeView view = Files.getFileAttributeView(path,
					AclFileAttributeView.class);
			List<AclEntry> aclEntryList = view.getAcl();
			for (AclEntry entry : aclEntryList) {
				System.out.println("User Principal Name: " + entry.principal().getName());
				System.out.println("ACL Entry Type: " + entry.type());
				displayEntryFlags(entry.flags());
				displayPermissions(entry.permissions());
				System.out.println();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void displayPermissions(Set<AclEntryPermission> permissionSet) {
		if (permissionSet.isEmpty()) {
			System.out.println("当下没有 Permissions ");
		} else {
			System.out.println("Permissions");
			for (AclEntryPermission permission : permissionSet) {
				System.out.print(permission.name() + " ");
			}
			System.out.println();
		}
	}

	private static void displayEntryFlags(Set<AclEntryFlag> flagSet) {
		if (flagSet.isEmpty()) {
			System.out.println("当下没有 ACL Entry Flags");
		} else {
			System.out.println("ACL Entry Flags");
			for (AclEntryFlag flag : flagSet) {
				System.out.print(flag.name() + " ");
			}
			System.out.println();
		}
	}
}
